package com.example.testscan;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ScanPrinterIndexActivity extends AppCompatActivity {
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_printer_index);
        init();

    }

    private void init() {

        webView = (WebView) findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.setVerticalScrollBarEnabled(true);

        WebSettings webSettings = webView.getSettings();
        // webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // 默认不使用缓存！
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        webSettings.setDomStorageEnabled(true);

        // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                // clearWebViewCache();
                view.loadUrl(url);
                return true;
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            // 覆写shouldOverrideUrlLoading实现内部显示网页
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO 自动生成的方法存根
                view.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl("file:///android_asset/www/scan_printer_index.html");

        // webView.loadUrl("file:///android_asset/www/amis_index_5.html");

        webView.addJavascriptInterface(new ScanPrinterIndexActivity.qscustomPrintprinter(), "customPrintAndroidInterface");

        //添加客户端支持
        webView.setWebChromeClient(new WebChromeClient());

    }


    boolean label1 = false;

    public class qscustomPrintprinter {

        @JavascriptInterface
        public void printText(int size, int align,boolean blackLabel, String str) {

            // 打印文字


        }

        @JavascriptInterface
        public void printQrCode(int align, int width, int height,boolean blackLabel,String str2) {


        }

        @JavascriptInterface
        public void printLabel(boolean label) {
            label1 = label;

        }

        @JavascriptInterface
        public void printBarCode(int align,  int width, int height, boolean blackLabel,String str2) {


        }

        @JavascriptInterface
        public void openScan() {
            //发送命令



        }

        @JavascriptInterface
        public void sendCMD(byte[] b) {
            //发送命令
        }

        @JavascriptInterface
        public void savecustomprintJson(String customprintJson) {
            //发送命令
            SharedPreferencesUtil.put(ScanPrinterIndexActivity.this,"customprintJson",customprintJson);
            String a="1";
            String b="2";
        }

        @JavascriptInterface
        public void printcustomprintJson(String customprintJson) {
            //发送命令
            // SharedPreferencesUtil.put(Custom_print_Activity.this,"customprintJson",customprintJson);
            String a="1";
            String b="2";
        }

    }
}